package com.hunan.api.catv.mq.consumer;

import com.hunan.api.catv.entity.enums.State;
import com.hunan.api.catv.entity.vo.NewsVo;
import com.hunan.api.catv.mq.MqConstants;
import com.hunan.api.catv.service.biz.news.NewsBiz;
import com.hunan.api.catv.service.biz.search.SearchBiz;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

@Component
public class NewsConsumer {

    public static Logger log = LoggerFactory.getLogger(NewsConsumer.class);

    @Autowired
    SearchBiz searchBiz;

    @Autowired
    NewsBiz newsBiz;

    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = MqConstants.news_queue, durable = "true"), exchange = @Exchange(value = MqConstants.news_exchange, durable = "true", type = "topic"), key = MqConstants.news_routingkey))
    public void news(Integer newsId) {
        log.info("[newsRefreshSolr-MQ: {} ]", newsId);
        try {
            if (!ObjectUtils.isEmpty(newsId)) {
                NewsVo newsVo = newsBiz.info(newsId);
                if (State.effective.getCode().equals(newsVo.getState())) {
                    searchBiz.add(newsVo);
                } else {
                    searchBiz.del(newsId);
                }
            }
        } catch (Exception e) {
            log.info("[newsRefreshSolr-MQ-Error:{}]", e);
        }
    }

    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = MqConstants.news_play_queue, durable = "true"), exchange = @Exchange(value = MqConstants.news_play_exchange, durable = "true", type = "topic"), key = MqConstants.news_play_routingkey))
    public void newsPlay(Integer newsId) {
        try {
            if (!ObjectUtils.isEmpty(newsId)) {
                newsBiz.incrCount(newsId, 2);
            }
        } catch (Exception e) {
            log.error("[newsPlay-MQ-Error:{}]", e);
        }
    }
}
